package com.example.teachmvandroidchart.base

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Application
import android.content.ContentValues
import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.graphics.Color
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.multidex.MultiDex
import com.example.teachmvandroidchart.util.CrashHandler
import com.example.teachmvandroidchart.util.SizeUtil
import com.billy.android.swipe.SmartSwipeBack
import com.billy.android.swipe.SwipeConsumer
import java.util.*
import com.example.databasetest.MyDatabaseHelper
import com.example.teachmvandroidchart.util.Utils
import java.text.DateFormat
import java.text.SimpleDateFormat

/**
 * Created by xuzhb on 2019/8/1
 * Desc:Application基类
 */
class BaseApplication : Application() {
    //创建数据库


    private var dbHelper: MyDatabaseHelper? = null


    private lateinit var mActivityStack: LinkedList<Activity>

    //获取Application单例
    companion object {
        lateinit var instance: BaseApplication
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        CrashHandler.instance.init(this)
        mActivityStack = LinkedList()
        initSwipeBack()
        dbHelper = MyDatabaseHelper(this, "temperature.db", null, 1)
        //Detele()

        Handler(Looper.getMainLooper()).postDelayed({
            /**
             * 数据初始化（200毫秒）AddDeviceActivity
             * */
            sql()
            //add()
        }, 300)

    }
    private fun Detele(){
        dbHelper?.deleteDatabase(applicationContext, "temperature.db");
    }
    @SuppressLint("Range")
    fun add(){

        val formatter = SimpleDateFormat("yyyy-MM-dd")
        val date: String = formatter.format(Date())
        /*
        * HH-mm-ss(24时制)
        * hh-mm-ss(12时制)
        * */
        val formatte = SimpleDateFormat("HH:mm:ss")
        val time = formatte.format(Date())

        val db: SQLiteDatabase = dbHelper!!.writableDatabase
        val values = ContentValues()
        // 开始组装第一条数据
        values.put("Home", (Math.random() * 100).toString())
        values.put("Date", date)
        values.put("time", time)
        values.put("temperatures", (Math.random() * 100).toFloat())
        values.put("hum", (Math.random() * 100).toFloat())
        db.insert("temp", null, values) // 插入第一条数据

        values.clear()
        //distinct：是否去重
        //columns：表示要查询的列名称，null表示查询所有列
        //selection：表示查询条件
        //selectionArgs：查询条件的值
        //groupBy：分组的列
        //having：分组条件
        //orderBy：查询结果的排序方式
        //limit：查询的数目
        //cancellationSignal：取消标记
        //Home text,Date text,time text,temperatures real
        //返回Cursor
        var sql : String = "select * from `temp` where `Date` = '2021-11-02';"
        val cursor: Cursor = db.rawQuery(sql,null)

        if (cursor.moveToFirst()) {
            do {
                val name: String = cursor.getString(
                    cursor.getColumnIndex("Date")
                )
                val author: String = cursor.getString(
                    cursor.getColumnIndex("time")
                )
                val pages: Float = cursor.getFloat(
                    cursor.getColumnIndex("temperatures")
                )
                Log.d("MainActivity", "book name is $name")
                Log.d("MainActivity", "book author is $author")
                Log.d("MainActivity", "book pages is $pages")
            } while (cursor.moveToNext())
        }
        cursor.close()

    }
    private fun sql() {

        dbHelper?.writableDatabase;
    }
    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

    //设置侧滑框架
    private fun initSwipeBack() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            SmartSwipeBack.activityBezierBack(this) { activity -> activity !is DontSwipeBack }
        } else {
            SmartSwipeBack.activitySlidingBack(
                this, { activity -> activity !is DontSwipeBack },
                SizeUtil.dp2pxInt(20f), 0, Color.parseColor("#88A3A3A3"),
                SizeUtil.dp2pxInt(15f), 0.5f, SwipeConsumer.DIRECTION_LEFT
            )
        }
    }

    fun addActivity(activity: Activity) {
        mActivityStack.add(activity)
    }

    fun removeActivity(activity: Activity) {
        if (mActivityStack.contains(activity)) {
            mActivityStack.remove(activity)
        }
    }

    //结束所有的Activity
    fun finishAllActivities() {
        for (i in mActivityStack.indices) {
            mActivityStack.get(i).finish()
        }
        mActivityStack.clear()
    }

}